package com.lp.service.impl;

import com.lp.dao.UserDao;
import com.lp.po.User;
import com.lp.po.UserExample;
import com.lp.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public User findUser(User user) {
        return userDao.findUser(user);
    }

    @Override
    public User findName(String username) {
        return userDao.findName(username);
    }

    //注册添加管理员
    @Override
    public int addUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public List<User> findUser() {
        return userDao.selectAll();
    }

    @Override
    public User findByName(String name) throws Exception {
        UserExample userloginExample = new UserExample();

        UserExample.Criteria criteria = userloginExample.createCriteria();
        criteria.andUsernameEqualTo(name);

        List<User> list = userDao.selectByExample(userloginExample);

        return list.get(0);
    }

    @Override
    public void updateById(@Param("username") String username, @Param("password")String password,@Param("role") Integer role, Integer userid) {
        userDao.updateById(username,password,role,userid);
    }

    @Override
    public User findUserIdWithUserName(String username) {
        return userDao.findUserIdWithUserName(username);
    }

    @Override
    public void updateWhitPassword(String password) {
        userDao.updateWhitPassword(password);
    }

    @Override
    public void updatePasswordWithStudent(String password,String username) {
        userDao.updateWhitStudent(password,username);
    }

    //根据用户名查找用户是否粗在
    @Override
    public User findUserByName(String username){
        return userDao.findUserByName(username);
    }

    @Override
    public int findUseridByStudentName(String username) {
        return userDao.findUseridByStudentName(username);
    }

    @Override
    public void updateByUserId(String username,Integer userid) {
        userDao.updateByUserId(username,userid);
    }
}
